package gudaziEx.service;

import commons.util.GameUtil;
import commons.util.ProcessDealUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.util.DigestUtils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class DownLoad {
    private static final Logger LOGGER = LogManager.getLogger(DownLoad.class);
    public static void downLoad(String HTTP_URL, String apkNamePath){
        BufferedInputStream  bis =null;
        BufferedOutputStream bos=null;
        try {
            int contentLength = getConnection(HTTP_URL).getContentLength();
            LOGGER.info("下载地址： "+HTTP_URL);
            LOGGER.info("下载路径： "+apkNamePath);
            LOGGER.info("文件大小： "+contentLength);
            if (contentLength>32) {
                InputStream is= getConnection(HTTP_URL).getInputStream();
//                String hex1 = DigestUtils.md5DigestAsHex(is);
                bis = new BufferedInputStream(is);
                FileOutputStream fos = new FileOutputStream(apkNamePath);
                bos= new BufferedOutputStream(fos);
                int b = 0;
                byte[] byArr = new byte[1024];
                LOGGER.info("下载进度:");
                int i = 0;
                int perGo = contentLength/20;
                int percentPoint=0;
                while((b= bis.read(byArr))!=-1){
                    i+=b;
                    bos.write(byArr, 0, b);
                    if(i%perGo < 1024){
                        if(percentPoint!=100){
                            percentPoint+=5;
                            LOGGER.info(percentPoint + " %");
                        }
                    }
                }
                long lastSize = new File(apkNamePath).length();
                int perCheck = (int) (lastSize*100/contentLength);
                LOGGER.info("文件匹配率: " + perCheck + " %");
                if(perCheck < 98){
                    LOGGER.info("文件不完整！重新下载！");
                    LOGGER.info(" ");
                    downLoad(HTTP_URL,apkNamePath);
                    return;
                }else{
                    LOGGER.info("文件匹配率通过！");
                }
                LOGGER.info("文件下载完成！");
            }

        } catch (Exception e) {
            LOGGER.info(e.getMessage());
            downLoad(HTTP_URL,apkNamePath);
        }finally{
            try {
                if(bis !=null){
                    bis.close();
                }
                if(bos !=null){
                    bos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static HttpURLConnection getConnection(String httpUrl) throws Exception {
        URL url = new URL(httpUrl);
        HttpURLConnection connection =  (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Content-Type", "application/octet-stream");
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setReadTimeout(5000);
        connection.connect();
        return connection;

    }
}
